﻿using BLL.Report;
using Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using YQFK.Models;

namespace YQFK.api
{
    /// <summary>
    /// 疫情上报
    /// </summary>
    public class ReportOnEpidemicController : ApiController
    {
        private readonly ReportOnEpidemicSituationBLL _Report;
        /// <summary>
        /// 疫情上报
        /// </summary>
        public ReportOnEpidemicController()
        {
            _Report = new ReportOnEpidemicSituationBLL();
        }
        /// <summary>
        /// 疫情上报列表分页查询
        /// </summary>
        /// <param name="jitem"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/ReportOnEpidemic/GetPagedList")]
        public object GetPagedList(JObject jitem)
        {
            try
            {
                int pageIndex = Convert.ToInt32(jitem["pageIndex"]);
                int pageSize = Convert.ToInt32(jitem["pageSize"]);
                string deptName = jitem["deptName"]?.ToString();
                string userName = jitem["userName"]?.ToString();
                string departId = jitem["departId"]?.ToString();
                string FillInDateStart = jitem["FillInDateStart"]?.ToString();
                string FillInDateEnd = jitem["FillInDateEnd"]?.ToString();
                int allCount = 0;
                List<ReportOnEpidemicSituationModel> data = _Report.GetDataList(departId, userName, deptName, FillInDateStart, FillInDateEnd, pageSize, pageIndex, out allCount).ToList();
                int ljsb = _Report.sum("FillInNum");
                int ljqz = _Report.sum("AddDiagnosisNum");
                int ljys = _Report.sum("AddSuspectedNum");
                int ljzy = _Report.sum("AddCureNum");
                List<object> filnaData = new List<object>();
                filnaData.Add(new
                {
                    ljsb,
                    ljqz,
                    ljys,
                    ljzy,
                    data
                }
                    );
                return new ResponeModel(0, "查询成功", filnaData, allCount);
            }
            catch (Exception ex)
            {
                return new ResponeModel(-1, $"操作失败：{ex.Message}");
            }
        }

        /// <summary>
        /// 疫情上报详情
        /// </summary>
        /// <param name="jitem"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/ReportOnEpidemic/GetSingleDataById")]
        public object GetSingleDataById(JObject jitem)
        {
            string id = jitem["id"]?.ToString();
            ReportOnEpidemicSituationModel thm = _Report.GetSingleDataById(id);
            if (thm == null || thm.Id.ToString() == "00000000-0000-0000-0000-000000000000")
            {
                var obj = new { code = 0, count = 0, info = "查询成功", date = new object() };
                var result = JsonConvert.SerializeObject(obj);
                return new HttpResponseMessage { Content = new StringContent(result, System.Text.Encoding.UTF8, "application/json") };
            }
            else
            {
                var obj = new { code = 0, count = 1, info = "查询成功", date = thm };
                var result = JsonConvert.SerializeObject(obj);
                return new HttpResponseMessage { Content = new StringContent(result, System.Text.Encoding.UTF8, "application/json") };
            }
        }

        /// <summary>
        /// 修改疫情
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("api/ReportOnEpidemic/Update")]
        public object Update(JObject jitem)
        {
            try
            {
                string id = jitem["id"]?.ToString();
                int AddDiagnosisNum = int.Parse(jitem["AddDiagnosisNum"]?.ToString());
                int AddCureNum = int.Parse(jitem["AddCureNum"]?.ToString());
                //var json = HttpContext.Current.Request["json"];
                //string jsonString = JsonConvert.SerializeObject(json);
                //ReportOnEpidemicSituationModel model = JsonConvert.DeserializeObject<ReportOnEpidemicSituationModel>(json);
                bool success = _Report.Update(id, AddDiagnosisNum, AddCureNum);
                HttpFileCollection files = HttpContext.Current.Request.Files;
                if (success)
                    return new ResponeModel();
                else
                    return new ResponeModel(-1, "操作失败");
            }
            catch (Exception ex)
            {
                return new ResponeModel(-1, $"操作失败：{ex.Message}");
            }
        }
    }
}